using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using lwzlz.Web.Filter;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [MenuFilter]
    public class StoreController : AdminController
    {
        public ActionResult Index()
        {
            List<Place> deep1List = new PlaceService().GetDeep1();
            ViewData["deep1List"] = deep1List;
            List<Place> deep2List = new PlaceService().GetDeep2();
            ViewData["deep2List"] = deep2List;
            List<Place> deep3List = new PlaceService().GetDeep3();
            ViewData["deep3List"] = deep3List;
            return View();
        }

        /// <summary>
        /// 获取首页列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetListPage()
        {
            LayTableModel result = new LayTableModel();
            StoreService service = new StoreService();
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量
                string field = Request.Params["field"];//排序字段名
                string order = Request.Params["order"];//排序方式：desc, asc, null(空对象，默认排序)
                //参数
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string[] par = { "Name", "Status", "ProvinceCode", "CityCode", "DistrictCode" };

                foreach (string item in par)
                {
                    if (!string.IsNullOrWhiteSpace(Request.Params[item]))
                    {
                        dic.Add(item, Request.Params[item]);
                    }
                }
                int count = 0;

                var list = service.GetListPage(page, limit, dic, field, order, out count);

                result.data = list;
                result.count = count;

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 添加修改视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult AddOrEdit(int Id = 0)
        {
            StoreOutput model = null;
            StoreService service = new StoreService();

            //省市区
            PlaceService ptService = new PlaceService();
            ViewBag.Province = ptService.GetSelectList("0");

            if (Id > 0)
            {
                model = service.Get<StoreOutput>(Id);
                ViewBag.City = ptService.GetSelectList(model.ProvinceCode);
                ViewBag.District = ptService.GetSelectList(model.CityCode);
            }
            else
            {
                model = new StoreOutput();
                ViewBag.City = "";
                ViewBag.District = "";
            }
            return View(model);
        }

        /// <summary>
        /// 查看视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult Look(int Id = 0)
        {
            StoreOutput model = null;
            StoreService service = new StoreService();
            if (Id > 0)
            {
                model = service.Get<StoreOutput>(Id);
            }
            else
            {
                model = new StoreOutput();
            }
            return View(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Add(StoreInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int result = new StoreService().Add(model);
                if (result > 0)
                {
                    response.Success = true;
                    response.Message = "添加成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "添加失败";
                }

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit(StoreInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int id = new StoreService().Edit(model);
                response.Success = true;
                response.Message = "修改成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Delete")]
        public JsonResult DeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new StoreService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Edit")]
        public JsonResult UpdateStatus(int Id)
        {
            ResponseModel response = new ResponseModel();
            StoreService serviece = new StoreService();
            TeacherService teacherService = new TeacherService();
            try
            {
                string status = Request.Params["status"];
                if (!string.IsNullOrWhiteSpace(status))
                {
                    if (status == "2")
                    {
                        var teacherList = teacherService.GetList(Id);
                        foreach (var teacher in teacherList)
                        {
                            teacherService.UpdateStatus(teacher.Id, 2);
                        }
                    }
                    serviece.UpdateStatus(Id, int.Parse(status));

                    response.Success = true;
                    response.Message = "修改成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "参数错误";
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
    }
}
